package com.lishe.lishe.lishe.module.excitation.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.base.common.adapter.CommonAdapter;
import com.lishe.base.common.adapter.MultiItemTypeAdapter;
import com.lishe.base.common.adapter.base.ViewHolder;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.GridDividerItemDecoration;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerGriadViewItemDecoration;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.bean.DianZanObject;
import com.lishe.lishe.lishe.module.excitation.bean.DianzanImgObject;
import com.lishe.lishe.lishe.module.excitation.interfaces.OnDianZanWallClickListener;
import com.lishe.lishe.lishe.utils.StringUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.lishe.widget.CircleImageView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by shino on 2017/10/21.
 */

public class DianZanWallAdapter extends RecyclerviewBaseAdapter {
    private final GridDividerItemDecoration decoration;
    private Activity context;
    private LinkedList<DianZanObject> dianzanList;
    private OnDianZanWallClickListener listener;
    private int mDisplayWidth;
    private boolean isScrolling = false;

    public DianZanWallAdapter(Activity context, LinkedList<DianZanObject> dianzanList, int displayWidth) {
        this.context = context;
        this.dianzanList = dianzanList;
        this.mDisplayWidth = displayWidth;
        decoration = new GridDividerItemDecoration(Utils.dp2px(5), false, false);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_give_like, null);
        DianZanViewHolder viewHolder = new DianZanViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {//当view没有绑定到screen时，payloads就会失效被丢弃
            onBindViewHolder(holder, position);
        } else {
            updateContent(holder, dianzanList.get(position));
        }
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, final int position) {
        final DianZanObject dianZanObject = dianzanList.get(position);
        if (dianZanObject.getCreateUserType().equals("0")) {
            ((DianZanViewHolder) holder).lable.setVisibility(View.VISIBLE);
        } else {
            ((DianZanViewHolder) holder).lable.setVisibility(View.GONE);
        }
        ImageLoaderUtils.displayCircleImage(context, dianZanObject.getAvatarPath(), ((DianZanViewHolder) holder).headImg, R.mipmap.head);
        if (!isScrolling) {
        } else
            ((DianZanViewHolder) holder).headImg.setImageResource(R.mipmap.head);
        initIamges(dianZanObject.getImgList(), (DianZanViewHolder) holder, position);

        updateContent(holder, dianZanObject);
        setListener(holder, position);
    }

    private void updateContent(RecyclerView.ViewHolder holder, DianZanObject dianZanObject) {
        ((DianZanViewHolder) holder).name.setText(dianZanObject.getEmpName());
        ((DianZanViewHolder) holder).occupation.setText(dianZanObject.getPosName());
        ((DianZanViewHolder) holder).company.setText(dianZanObject.getCreateUserName());
        ((DianZanViewHolder) holder).content.setText(StringUtils.utf8ToString(dianZanObject.getEventDesc()));
        ((DianZanViewHolder) holder).note.setText(dianZanObject.getActionName());
        ((DianZanViewHolder) holder).forward.setText(dianZanObject.getShareCount() == 0 ? "转发" : dianZanObject.getShareCount() + "");
        ((DianZanViewHolder) holder).comment.setText(dianZanObject.getCommentCount() == 0 ? "评论" : dianZanObject.getCommentCount() + "");
        ((DianZanViewHolder) holder).gift.setText(dianZanObject.getGiftCount() == 0 ? "礼物" : dianZanObject.getGiftCount() + "");

        if (dianZanObject.isFlag()) {
            setTextDrawable(((DianZanViewHolder) holder).dianzan, R.mipmap.heart_red);
            ((DianZanViewHolder) holder).dianzan.setTextColor(context.getResources().getColor(R.color.color_ff4138));
        } else {
            setTextDrawable(((DianZanViewHolder) holder).dianzan, R.mipmap.heart_black);
            ((DianZanViewHolder) holder).dianzan.setTextColor(context.getResources().getColor(R.color.color_999999));
        }
        ((DianZanViewHolder) holder).dianzan.setText(dianZanObject.getAdmiredCount() == 0 ? "鼓励" : dianZanObject.getAdmiredCount() + "");
    }

    private void setTextDrawable(TextView dianzan, int heart_red) {
        Drawable drawable = context.getResources().getDrawable(heart_red);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        dianzan.setCompoundDrawables(drawable, null, null, null);
    }

    private void setListener(RecyclerView.ViewHolder holder, final int position) {
        ((DianZanViewHolder) holder).forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickForward(position);
            }
        });
        ((DianZanViewHolder) holder).comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickComment(position);
            }
        });
        ((DianZanViewHolder) holder).gift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickGift(position);
            }
        });
        ((DianZanViewHolder) holder).dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (listener != null) listener.onClickDianZan(position);
            }
        });
        ((DianZanViewHolder) holder).headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onClickHead(dianzanList.get(position).getUserId());
            }
        });
        ((DianZanViewHolder) holder).name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onClickHead(dianzanList.get(position).getUserId());
            }
        });
        ((DianZanViewHolder) holder).company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (!dianzanList.get(position).getCreateUserType().equals("0"))
                        listener.onClickHead(dianzanList.get(position).getCreateUserId());
                }
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return dianzanList == null ? 0 : dianzanList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    public void setScrolling(boolean scrolling) {
        this.isScrolling = scrolling;
    }

    class DianZanViewHolder extends RecyclerView.ViewHolder {
        private ImageView headImg;
        private TextView name;
        private TextView occupation;
        private TextView company;
        private TextView content;
        private TextView forward;
        private TextView gift;
        private TextView comment;
        private TextView dianzan;
        private ImageView lable;
        private RecyclerView imageRecycleView;
        private TextView note;
        private CommonAdapter<DianzanImgObject> imageAdapter;
        private ArrayList<DianzanImgObject> imgs = new ArrayList<>();

        public DianZanViewHolder(View itemView) {
            super(itemView);
            headImg = itemView.findViewById(R.id.head_img);
            name = itemView.findViewById(R.id.name);
            occupation = itemView.findViewById(R.id.occupation);
            company = itemView.findViewById(R.id.company);
            content = itemView.findViewById(R.id.content);
            imageRecycleView = itemView.findViewById(R.id.image_recyclerView);
//            forwardLayout = itemView.findViewById(R.id.forward_layout);
            forward = itemView.findViewById(R.id.forward);
//            giftLayout = itemView.findViewById(R.id.gift_layout);
            gift = itemView.findViewById(R.id.gift);
            comment = itemView.findViewById(R.id.comment);
            dianzan = itemView.findViewById(R.id.dianzan);
            lable = itemView.findViewById(R.id.lable);
            note = itemView.findViewById(R.id.note);

            imageAdapter = new CommonAdapter<DianzanImgObject>(context, R.layout.item_round_image_view, imgs) {
                @Override
                protected void convert(ViewHolder holder, DianzanImgObject dianzanImgObject, int position) {
                    int width = (mDisplayWidth - Utils.dip2px(context, 40)) / 3;
                    ViewGroup.LayoutParams layoutParams = holder.getView(R.id.image_layout).getLayoutParams();
                    layoutParams.width = width;
                    layoutParams.height = width;
                    if (isScrolling)
                        holder.setImageResource(R.id.image_round, R.drawable.defalut_img);
                    else
                        ImageLoaderUtils.setImageResource(context, dianzanImgObject.getImgUrl(), holder.getView(R.id.image_round), R.drawable.defalut_img);
                }
            };
        }

        public void setData(ArrayList<DianzanImgObject> imgs) {
            this.imgs.clear();
            this.imgs.addAll(imgs);
            if (imageAdapter != null)
                imageAdapter.notifyDataSetChanged();
        }
    }

    private void initIamges(ArrayList<DianzanImgObject> imgs, DianZanViewHolder holder, int parentPosition) {
        holder.imageRecycleView.setVisibility(View.VISIBLE);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }

            @Override
            protected int getExtraLayoutSpace(RecyclerView.State state) {
                return 300;
            }
        };
        layoutManager.setItemPrefetchEnabled(true);
        layoutManager.collectInitialPrefetchPositions(3, (layoutPosition, pixelDistance) -> {
        });
        holder.imageRecycleView.setLayoutManager(layoutManager);
        holder.imageRecycleView.removeItemDecoration(decoration);
        holder.imageRecycleView.addItemDecoration(decoration);
        holder.imageRecycleView.setItemViewCacheSize(15);
        holder.setData(imgs);
        holder.imageAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (listener != null)
                    listener.onClickImage(parentPosition, position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        holder.imageAdapter.notifyDataSetChanged();
        holder.imageRecycleView.setAdapter(holder.imageAdapter);
    }

    public void setOnDianZanWallClickListener(OnDianZanWallClickListener listener) {
        this.listener = listener;
    }

    public void unRegisterOnDianZanWallClickListener(OnDianZanWallClickListener listener) {
        this.listener = null;
    }
}
